package com.htfyun.ft.fragment.engineer;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Switch;

import com.htfyun.ft.R;
import com.htfyun.ft.fragment.BaseFragment;
import com.htfyun.hardware.LedController;
import com.htfyun.serialport.ServoSingleton;

import androidx.annotation.Nullable;


/**
 * Created by sst on 2018/7/11.
 */

public class FragmentLed extends BaseFragment
        implements View.OnClickListener {

    private final static String TAG = "FragmentLed";

    private EditText editBrightness;

    private EditText editMarqueeTime;
    private EditText editMarqueeLedNumber;
    private EditText editMarqueeLoopCount;
    private CheckBox checkBoxMarqueeAnticlockwise;

    private Switch switchLedSuspend;

    private Spinner spinnerLedBreath;

    private Switch switchEnabled;

    @Override
    protected int getLayoutRes() {
        return R.layout.fragment_engineer_led;
    }

    @Override
    protected void onInitView(@Nullable Bundle savedInstanceState) {

        initBtn(R.id.btnAllOff);
        initBtn(R.id.btnAllOn);
        initBtn(R.id.btnEvenOn);
        initBtn(R.id.btnOddOn);
        initBtn(R.id.btnMarqueeGo);

        editBrightness = (EditText) findViewById(R.id.editBrightness);
        editMarqueeTime = (EditText) findViewById(R.id.editMarqueeTime);
        editMarqueeLedNumber = (EditText) findViewById(R.id.editMarqueeLedNumber);
        editMarqueeLoopCount = (EditText) findViewById(R.id.editMarqueeLoopCount);
        checkBoxMarqueeAnticlockwise = (CheckBox) findViewById(R.id.checkBoxMarqueeAnticlockwise);
        initCheckBoxMarqueeAnticlockwise();


        switchLedSuspend = (Switch) findViewById(R.id.switchLedSuspend);
        initSwitchLedSuspend();

        spinnerLedBreath = (Spinner) findViewById(R.id.spinnerLedBreath);
//
//        List<String> breathModeList = new ArrayList<String>();
//        breathModeList.add("Faster");
//        breathModeList.add("Fast");
//        breathModeList.add("Slow");
//        breathModeList.add("Slower");
//
//        ArrayAdapter<String> adapter = new ArrayAdapter<String>(getContext(), android.R.layout.simple_spinner_item, breathModeList);
//        spinnerLedBreath.setAdapter(adapter);
        spinnerLedBreath.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                if (switchEnabled.isChecked()) {
                    LedController.LedBreathModeAttr attr = LedController.LedBreathModeAttr.toMode(position);
                    LedController.INSTANCE.setLedBreath(attr, 0);
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

        initSwitch();

    }


    private void initSwitch() {
        switchEnabled = (Switch) findViewById(R.id.switchEnabled);
        switchEnabled.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                setAllViewEnabled(isChecked);
            }
        });

        switchEnabled.setChecked(false);
        setAllViewEnabled(false);

    }

    private void setAllViewEnabled(boolean enabled) {

        View container = findViewById(R.id.container);
        setViewEnabled(container, enabled);

        switchEnabled.setEnabled(true);

    }

    private void setViewEnabled(View view, boolean enabled) {
        if (view == null) return;

        if (view instanceof ViewGroup) {
            ViewGroup viewGroup = (ViewGroup) view;

            for (int i = 0; i < viewGroup.getChildCount(); i++) {
                View child = viewGroup.getChildAt(i);
                setViewEnabled(child, enabled);
            }

            return;
        }

        view.setEnabled(enabled);

    }



    private void initBtn(int resId) {
        View btn = findViewById(resId);
        if (btn != null) {
            btn.setOnClickListener(this);
        }
    }

    private void initSwitchLedSuspend() {
        if (switchLedSuspend == null) {
            return;
        }
        switchLedSuspend.setOnCheckedChangeListener(null);
        switchLedSuspend.setChecked(LedController.INSTANCE.isOffInSuspend());
        switchLedSuspend.setOnCheckedChangeListener(onCheckedChangeListener);
    }

    private void initCheckBoxMarqueeAnticlockwise() {
        if (checkBoxMarqueeAnticlockwise == null) {
            return;
        }
        checkBoxMarqueeAnticlockwise.setOnCheckedChangeListener(null);
        checkBoxMarqueeAnticlockwise.setChecked(LedController.INSTANCE.isMarqueeAnticlockwise());
        checkBoxMarqueeAnticlockwise.setOnCheckedChangeListener(onCheckedChangeListener);

    }

    @Override
    protected void onVisible() {
        super.onVisible();

        initCheckBoxMarqueeAnticlockwise();
        initSwitchLedSuspend();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        LedController.INSTANCE.setAllLedBrightness(0);
    }

    private CompoundButton.OnCheckedChangeListener onCheckedChangeListener =
            new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                    if (buttonView == checkBoxMarqueeAnticlockwise) {
                        LedController.INSTANCE.setMarqueeAnticlockwise(isChecked);
                    } else if (buttonView == switchLedSuspend) {
                        LedController.INSTANCE.setOffInSuspend(isChecked);
                    }

                }
            };


    @Override
    public void onClick(View v) {
        final int id = v.getId();

        int brightness = 0xff;
        String brightnessStr = editBrightness.getText().toString();
        if (TextUtils.isDigitsOnly(brightnessStr)) {
            brightness = Integer.decode(brightnessStr);
        }

        switch (id) {
            case R.id.btnAllOff:
                LedController.INSTANCE.setAllLedBrightness(0);
                break;
            case R.id.btnAllOn:
                LedController.INSTANCE.setAllLedBrightness(brightness);
                break;

            case R.id.btnEvenOn: {
                int bits = 0;
                for (int i = 0; i < LedController.MAX_LED_NUMBER; i++) {
                    if (i % 2 == 0) {
                        bits |= 1 << i;
                    }
                }
                LedController.INSTANCE.setAllLedBrightness(0);
                LedController.INSTANCE.setLedBitBrightness(bits, brightness);

                break;
            }
            case R.id.btnOddOn: {
                int bits = 0;
                for (int i = 0; i < LedController.MAX_LED_NUMBER; i++) {
                    if (i % 2 != 0) {
                        bits |= 1 << i;
                    }
                }
                LedController.INSTANCE.setAllLedBrightness(0);
                LedController.INSTANCE.setLedBitBrightness(bits, brightness);
                break;
            }
            case R.id.btnMarqueeGo:
                int time_ms = 100;
                String timeStr = editMarqueeTime.getText().toString();
                if (TextUtils.isDigitsOnly(timeStr)) {
                    time_ms = Integer.decode(timeStr);
                }
                int ledNumber = 0;
                String ledNumberStr = editMarqueeLedNumber.getText().toString();
                if (TextUtils.isDigitsOnly(ledNumberStr)) {
                    ledNumber = Integer.decode(ledNumberStr);
                }
                int loopCount = 5;
                String loopCountStr = editMarqueeLoopCount.getText().toString();
                if (TextUtils.isDigitsOnly(loopCountStr)) {
                    loopCount = Integer.decode(loopCountStr);
                }

                LedController.INSTANCE.setMarquee(time_ms, loopCount, ledNumber);

                break;

                default:break;
        }
    }



}
